package com.excesys.exsecs.om.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/websocket/{name}")
@Slf4j
public class WebSocketSend {

	// 与某个客户端连接对话，通过此对客户端发送消息
	private Session session;

	private String sessionName;

	// 存放所有连接的客户端
	private static ConcurrentHashMap<String, WebSocketSend> webSocketConcurrentHashMap = new ConcurrentHashMap<>();

	private static Map map = new HashMap<>();

	@OnOpen
	public void onOpen(Session session, @PathParam(value = "name") String name) {
		// 默认客户端，没有重名
		this.session = session;
		this.sessionName = name;
		webSocketConcurrentHashMap.put(name, this);
		map.put(name, name);
		System.out.println("【webSocket连接成功】当前连接人数为ddddd：" + webSocketConcurrentHashMap.size() + "，此人为：" + name);
		log.info("【webSocket连接成功】当前连接人数为ddddd：" + webSocketConcurrentHashMap.size() + "，此人为：" + name);
		log.info("【webSocket连接成功】当前连接人数为ddddd：" + map.size() + "，此人为：" + name);

	}

	@OnClose
	public void onClose() {

		// 这里判断不周，仅实验用

		for (String name : webSocketConcurrentHashMap.keySet()) {

			if (this == webSocketConcurrentHashMap.get(name)) {

				webSocketConcurrentHashMap.remove(name);
				break;
			}
		}

		log.info("【webSocket退出成功】当前连接人数为：" + webSocketConcurrentHashMap.size());
		System.out.println("【webSocket退出成功】当前连接人数为：" + webSocketConcurrentHashMap.size());
	}

	@OnError
	public void onError(Session session, Throwable throwable) {

		System.out.println("error:");
		throwable.printStackTrace();
	}

	@OnMessage
	public void onMessage(Session session, String message) {

		System.out.println("【webSocket接收成功】内容为：" + message);
		log.info("【webSocket接收成功】内容为：" + message);
		System.out.println(webSocketConcurrentHashMap.keySet());
		// 此处可以指定发送，或者群发，或者xxxx的

		// if(message.indexOf("name:") == 0){

		String name = message.split("/")[0];
		String txt = message.split("/")[1];

		appointSending(name, txt);

	}

	// 群发
	public void groupSending(String message, Session exIncludeSession) {

		for (String name : webSocketConcurrentHashMap.keySet()) {

			try {
				if (exIncludeSession == webSocketConcurrentHashMap.get(name).session)
					continue;

				webSocketConcurrentHashMap.get(name).session.getBasicRemote().sendText(name + ":" + message);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 指定发
	public void appointSending(String name, String message) {

		try {
			log.info("【webSocket外】" + name);
			log.info("webSocketConcurrentHashMap:" + webSocketConcurrentHashMap);
			log.info("webSocketConcurrentHashMap:" + webSocketConcurrentHashMap.get(name));
			log.info("【webSocket连接成功】Map：" + map.size() + "，此人为：" + map.get(name));
			if (webSocketConcurrentHashMap.get(name) != null) {
				log.info("【webSocket内】");
				log.info("webSocketConcurrentHashMap:" + webSocketConcurrentHashMap);
				webSocketConcurrentHashMap.get(name).session.getBasicRemote().sendText(message);
				log.info("【webSocket内name】" + name);
				log.info("【webSocket内message】" + message);
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Session getSession() {
		return session;
	}

}